const fs = require('fs');
const parser = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const tee = require('@babel/types');
const generator = require("@babel/generator").default;



var cbbiyhh = () => {
    var text = fs.readFileSync('./weipu.js', {
        encoding: "utf-8"
    });
    var ast = parser.parse(text.toString(), {
        sourceType: "module"
    });

    var allverList = ['String.fromCharCode'];

    var astWeiPu = {
        VariableDeclarator(path) {
            if (allverList.indexOf(generator(path.node.init).code) != -1 && tee.isIdentifier(path.node.init) && tee.isIdentifier(path.node.id)) {
                console.log(generator(path.node.init).code)
                var binDing = path.scope.getBinding(path.node.id.name);
                for (let i = 0; i < binDing.referencePaths.length; i++) {
                    binDing.referencePaths[i].replaceWith(path.node.init)
                }
                ;
            }
            ;
        },
    };

    traverse(ast, astWeiPu);

    astText = generator(ast, {
        minified: false,
        jsescOption: {
            minimal: true
        }
    }).code;

    var ast = parser.parse(astText, {
        sourceType: "module"
    });


    traverse(ast, {
        VariableDeclarator(path) {
            if (generator(path.node.init).code == "String.fromCharCode") {
                if (tee.isArrayExpression(path.container[0].init) && path.container[0].init.elements.length == 0 && tee.isArrayExpression(path.container[1].init) && path.container[1].init.elements.length == 0) {
                    console.log(1)
                } else {
                    return;
                }
                var jieHxDeName = [];
                jieHxDeName.push(path.container[0].id.name);
                jieHxDeName.push(path.container[1].id.name);
                var jiamiName = path.parentPath.container[path.parentPath.key + 2];
                var needUseList = path.parentPath.container.slice(path.parentPath.key, path.parentPath.key + 4);
                let jianMi = path.scope.getBinding(jiamiName.expression.callee.name).path;

                let veryListHans = jianMi.node.body.body[1].expression.right.callee.object.callee.name
                let jianMi2xg = path.scope.getBinding(veryListHans)

                let jianMi2 = jianMi2xg.path;

                jiamiName = path.parentPath.container[path.parentPath.key + 3];

                let jianMi3 = path.scope.getBinding(jiamiName.expression.callee.name).path;

                eval(jianMi2 + "")
                eval(jianMi + "")
                eval(jianMi3 + "")
                for (let i = 0; i < needUseList.length; i++) {
                    eval(generator(needUseList[i]).code)
                }
                for (let i = 0; i < jieHxDeName.length; i++) {
                    let habnuu = path.scope.getBinding(jieHxDeName[i]);
                    for (let yhh = 0; yhh < habnuu.referencePaths.length; yhh++) {
                        if (!(habnuu.referencePaths[yhh].parentPath.isMemberExpression() && tee.isNumericLiteral(habnuu.referencePaths[yhh].parentPath.node.property))) {
                            continue
                        }
                        ;
                        // try {
                        habnuu.referencePaths[yhh].parentPath.replaceWith(tee.valueToNode(eval(habnuu.referencePaths[yhh].parentPath + "")));
                        path.stop()
                        // }catch (e) {
                        //     debugger;
                        // }
                    }
                }
                ;
                //["ceil"](_$nD.length
                for (let p = 0; p < jianMi2xg.referencePaths.length; p++) {
                    if (jianMi2xg.referencePaths[p].parentPath.isCallExpression() && jianMi2xg.referencePaths[p].parentPath.node.arguments.length == 1 && tee.isStringLiteral(jianMi2xg.referencePaths[p].parentPath.node.arguments[0])) {
                        jianMi2xg.referencePaths[p].parentPath.replaceWith(tee.valueToNode(eval(jianMi2xg.referencePaths[p].parentPath + "")))
                    }
                }
                ;
                path.stop();
            }
            ;
        },
    });

    astText = generator(ast, {
        minified: false,
        jsescOption: {
            minimal: true
        }
    }).code;


    var ast = parser.parse(astText, {
        sourceType: "module"
    });

    var getOver = {
        ArrayExpression(path) {
            if (path.parentPath.isVariableDeclarator() && path.node.elements.length != 0) {
                var banding = path.scope.getBinding(path.parent.id.name);
                if (banding.constantViolations.length != 0) {
                    return;
                }
                for (var i = 0; i < banding.referencePaths.length; i++) {
                    try {
                        banding.referencePaths[i].parentPath.replaceWith(path.node.elements[banding.referencePaths[i].parent.property.value]);
                    } catch (e) {

                    }
                    ;
                }
                ;
            }
            ;
        },
    }

    traverse(ast, getOver);

    astText = generator(ast, {
        minified: false,
        jsescOption: {
            minimal: true
        }
    }).code;

    for (let i = 0; i < 5; i++) {
        ast = parser.parse(astText, {
            sourceType: "module"
        })


        traverse(ast, {
            BinaryExpression(path) {
                let left = path.node.left;
                let right = path.node.right;
                if ((tee.isNumericLiteral(left) || tee.isUnaryExpression(left)) && (tee.isNumericLiteral(right) || tee.isUnaryExpression(right))) {
                    try {
                        let {confident, value} = path.evaluate();
                        confident && path.replaceWith(tee.valueToNode(value));
                    } catch (e) {

                    }
                }
                ;
            },
            ReturnStatement(path) {
                if (tee.isSequenceExpression(path.node.argument)) {
                    expressions = path.node.argument.expressions;
                    popExpressions = expressions.pop();
                    expressions.map((v) => {
                        path.insertBefore(tee.ExpressionStatement(v));
                    });
                    path.node.argument = popExpressions;
                }
            }
        });
        break
    };


    astText = generator(ast, {
        minified: true,
        jsescOption: {
            minimal: true
        }
    }).code;
    fs.writeFile('./weipu2.js',
        astText, (err) => {
        }
    )
    console.log(1);
    return 1;
};

cbbiyhh()
